package cn.fintecher.pangolin.service.manage.request;

import cn.fintecher.pangolin.common.enums.BaseSelect;
import cn.fintecher.pangolin.common.enums.UserStatus;
import cn.fintecher.pangolin.entity.mysql.QUser;
import com.querydsl.core.BooleanBuilder;
import io.swagger.annotations.ApiModelProperty;
import lombok.Data;
import org.apache.commons.lang.StringUtils;

import java.util.Objects;

/**
 * @Auther: xiaqun
 * @Description: 用户查询参数
 * @Date: 19:26 2019/8/14
 */

@Data
public class FindUserRequest {
    @ApiModelProperty(value = "用户名")
    private String userName;

    @ApiModelProperty(value = "姓名")
    private String realName;

    @ApiModelProperty(value = "状态")
    private UserStatus status;

    @ApiModelProperty(value = "角色ID")
    private String roleId;

    @ApiModelProperty(value = "部门ID")
    private String departId;

    @ApiModelProperty(value = "部门code")
    private String departCode;

    @ApiModelProperty(value = "是否查询所有")
    private Integer isShowAll;

    public BooleanBuilder getBuilder() {
        BooleanBuilder builder = new BooleanBuilder();
        QUser qUser = QUser.user;
        builder.and(qUser.isSystem.eq(BaseSelect.NO));
        if (StringUtils.isNotBlank(this.userName)) {
            builder.and(qUser.username.like("%" + StringUtils.trim(this.userName) + "%"));
        }
        if (StringUtils.isNotBlank(this.realName)) {
            builder.and(qUser.realName.like("%" + StringUtils.trim(this.realName) + "%"));
        }
        if (Objects.nonNull(this.status)) {
            builder.and(qUser.status.eq(this.status));
        }
        if (Objects.nonNull(this.roleId)) {
            builder.and(qUser.roles.any().id.eq(this.roleId));
        }
        if (Objects.nonNull(this.isShowAll) && this.isShowAll == 1) {
            if (StringUtils.isNotBlank(this.departCode)) {
                builder.and(qUser.department.departCode.like(this.departCode + "%"));
            }
        } else {
            if (StringUtils.isNotBlank(this.departId)) {
                builder.and(qUser.department.id.eq(this.departId));
            }
        }
        return builder;
    }
}
